//
//  LAURefreshView.swift
//  刷新控件的项目准备
//
//  Created by Lausen on 2017/5/6.
//  Copyright © 2017年 id. All rights reserved.
//

import UIKit

class LAURefreshView: UIView {

    var parentViewHeight: CGFloat = 0
    
    var refreshState: LAURefreshState = .refreshStateNormal{
        
        /**
         1:苹果动画设置旋转式,会顺时针,并且有一个就近原则,可以让某一次旋转少那么一丢丢
         2:如果用UIView设置360度旋转的话,是设置不了的,不要用到CABasicAnimation
         */
        didSet{
            
            switch refreshState {
            case .refreshStateNormal:
                iconImageView?.isHidden = false
                indicator?.stopAnimating()
                tipLabel?.text = "继续往下拉..."
                UIView.animate(withDuration: 0.25, animations: {
                    self.iconImageView?.transform = CGAffineTransform.identity
                })
                
            case .refreshStatePulling:
                tipLabel?.text = "松手就刷新..."
                UIView.animate(withDuration: 0.25, animations: { 
                    self.iconImageView?.transform = CGAffineTransform.init(rotationAngle: CGFloat(M_PI + 0.0001))
                })
            case .refreshStateWillRefresh:
                iconImageView?.isHidden = true
                tipLabel?.text = "正在刷新中..."
                indicator?.startAnimating()
            }
        }
    }
    
    /// 刷新的箭头
    @IBOutlet weak var iconImageView: UIImageView?
    /// 刷新的label
    @IBOutlet weak var tipLabel: UILabel?
    /// 刷新的指示器
    @IBOutlet weak var indicator: UIActivityIndicatorView?
    
    class func refreshView() -> LAURefreshView{
        
        let nib = UINib.init(nibName: "LAUMeiTuanRefresh", bundle: nil)
        
        let refreshView = nib.instantiate(withOwner: nil, options: nil)[0] as! LAURefreshView
        
        return refreshView
    }
}
